<template>
  <a-card :bordered="false">
    <!-- 操作按钮区域 -->
    <a-space style="margin-bottom: 12px">
      <a-button @click="handleGeneratePicking" type="primary" icon="plus">生成领料单</a-button>
      <a-button @click="handleGenerateProductIn" type="primary" icon="plus">生成入库单</a-button>
      <div>
        <HfButton
          @click="
            () => {
              if (!this.selectedRowKeys.length) {
                $message.warning('请选择要生成返工单的工单')
                return
              }
              modal.rework.visible = true
            }
          "
          type="primary"
          icon="picking"
          >生成返工单
        </HfButton>
        <a-modal
          :visible="modal.rework.visible"
          @cancel="
            () => {
              modal.rework.visible = false
              modal.rework.step = ''
            }
          "
          :width="modal.rework.width"
          @ok="handleGenerateRework"
        >
          <a-form-model
            :model="modal.rework"
            :labelCol="{ span: 8 }"
            :wrapperCol="{ span: 14 }"
            :rules="modal.rework.rules"
            ref="formRef"
            style="margin: 0"
          >
            <a-form-model-item label="请选择返工工序" prop="step">
              <j-dict-select-tag v-model:value="modal.rework.step" dict-code="hf_rework_step" />
            </a-form-model-item>
            <a-form-model-item label="请选择返工领料仓" prop="pikingWarehouse">
              <j-dict-select-tag
                v-model:value="modal.rework.pikingWarehouse"
                dict-code="hf_warehouse,warehouse_name,id"
              />
            </a-form-model-item>
            <a-form-model-item label="请选择良品收料仓" prop="goodWarehouse">
              <j-dict-select-tag
                v-model:value="modal.rework.goodWarehouse"
                dict-code="hf_warehouse,warehouse_name,id"
              />
            </a-form-model-item>
            <a-form-model-item label="请选择不良品收料仓" prop="defectiveWarehouse">
              <j-dict-select-tag
                v-model:value="modal.rework.defectiveWarehouse"
                dict-code="hf_warehouse,warehouse_name,id"
              />
            </a-form-model-item>
            <a-form-model-item label="请选择报废品收料仓" prop="scrapWarehouse">
              <j-dict-select-tag
                v-model:value="modal.rework.scrapWarehouse"
                dict-code="hf_warehouse,warehouse_name,id"
              />
            </a-form-model-item>
            <a-form-model-item v-show="false" label="请选择部门" prop="depart">
              <j-dict-select-tag v-model:value="modal.rework.depart" dict-code="hf_warehouse,warehouse_name,id" />
            </a-form-model-item>
          </a-form-model>
        </a-modal>
      </div>
      <a-button v-if="selectedRowKeys.length > 0" type="danger" @click="batchDel">
        批量删除
        <a-icon type="delete" />
      </a-button>
    </a-space>

    <!-- table区域-begin -->
    <div>
      <a-table
        ref="table"
        size="middle"
        :scroll="{ x: true }"
        bordered
        rowKey="id"
        :columns="columns"
        :dataSource="dataSource"
        :pagination="false"
        :loading="loading"
        :rowSelection="{ selectedRowKeys: selectedRowKeys, onChange: onSelectChange }"
        class="j-table-force-nowrap"
        @change="handleTableChange"
      >
      </a-table>
    </div>
    <GeneratePickingPreview
      :source-id="mainId"
      :source-serial="sourceSerial"
      :orderIds="selectedRowKeys"
      ref="genPicking"
    ></GeneratePickingPreview>
    <GenerateProductPreview
      :source-id="mainId"
      :source-serial="sourceSerial"
      ref="genProductIn"
    ></GenerateProductPreview>
  </a-card>
</template>

<script>
import '@/assets/less/TableExpand.less'
import { mixinDevice } from '@/utils/mixin'
import { JeecgListMixin } from '@/mixins/JeecgListMixin'
import GenerateProductPreview from '@views/production/modules/generateProduct/GenerateProductPreview.vue'
import GeneratePickingPreview from '@views/production/modules/generatePicking/GeneratePickingPreview.vue'
import { getAction, postAction } from '@api/manage'
import { reworkIcon } from '@/icons'
import HfButton from '@comp/HfManual/HfButton.vue'
import { Modal } from 'ant-design-vue'
import Vue from 'vue'

export default {
  name: 'HfProductOrderList',
  mixins: [JeecgListMixin, mixinDevice],
  components: {
    HfButton,
    GeneratePickingPreview,
    GenerateProductPreview,
    reworkIcon
  },
  provide() {
    return {}
  },
  props: {
    mainId: {
      type: String,
      default: '',
      required: false
    },
    sourceSerial: {
      type: String,
      default: '',
      required: false
    }
  },
  watch: {
    mainId: {
      immediate: true,
      handler(val) {
        if (!this.mainId) {
          this.clearList()
        } else {
          this.queryParam['productionMissionId'] = val
          this.loadData()
        }
      }
    }
  },
  data() {
    return {
      description: '生产任务单子工单管理页面',
      disableMixinCreated: true,
      isChecked: false,
      modal: {
        rework: {
          visible: false,
          width: 600,
          step: '',
          goodWarehouse: '1777570854840557569',
          defectiveWarehouse: '1777570899157573633',
          scrapWarehouse: '1777570942979661825',
          pikingWarehouse: '1777570899157573633',
          depart: '1fb9c355c7f04ed0815666815008931a',
          rules: {
            step: [{ required: true, trigger: 'change', message: '请选择返工工序' }],
            pikingWarehouse: [{ required: true, trigger: 'change', message: '请选择返工领料仓' }],
            defectiveWarehouse: [{ required: true, trigger: 'change', message: '请选择不良品收料仓' }],
            scrapWarehouse: [{ required: true, trigger: 'change', message: '请选择报废品收料仓' }],
            goodWarehouse: [{ required: true, trigger: 'change', message: '请选择良品收料仓' }]
          }
        }
      },
      // 表头
      columns: [
        {
          title: '#',
          dataIndex: '',
          key: 'rowIndex',
          width: 60,
          align: 'center',
          customRender: function(t, r, index) {
            return parseInt(index) + 1
          }
        },
        {
          title: '创建时间',
          align: 'center',
          dataIndex: 'createTime'
        },
        {
          title: '工序',
          align: 'center',
          dataIndex: 'stepId_dictText'
        },
        {
          title: '产线',
          align: 'center',
          dataIndex: 'productionLine_dictText'
        },
        {
          title: '班次',
          align: 'center',
          dataIndex: 'workShift_dictText'
        },
        {
          title: '生产单位',
          align: 'center',
          dataIndex: 'productionDepart_dictText'
        },
        {
          title: '物料代码',
          align: 'center',
          dataIndex: 'materialCode'
        },
        {
          title: '物料名称',
          align: 'center',
          dataIndex: 'materialName'
        },
        {
          title: '规格型号',
          align: 'center',
          dataIndex: 'sap'
        },
        {
          title: '单位',
          align: 'center',
          dataIndex: 'unit_dictText'
        },
        {
          title: '物料类型',
          align: 'center',
          dataIndex: 'materialType_dictText'
        },
        {
          title: '仓库',
          align: 'center',
          dataIndex: 'warehouse_dictText'
        },
        {
          title: '计划生产数量',
          align: 'center',
          dataIndex: 'planProduceNumber'
        },
        {
          title: '实际生产数量',
          align: 'center',
          dataIndex: 'actualProduceNumber'
        }
        // {
        //   title: '操作',
        //   dataIndex: 'action',
        //   align:"center",
        //   fixed:"right",
        //   width:147,
        //   scopedSlots: { customRender: 'action' }
        // }
      ],
      url: {
        list: '/production/hfProductOrder/list',
        delete: '/production/hfProductOrder/delete',
        deleteBatch: '/production/hfProductOrder/deleteBatch',
        exportXlsUrl: '/production/hfProductOrder/exportXls',
        importExcelUrl: 'production/hfProductOrder/importExcel',
        generateRework: '/production/hfRework/generateRework'
      }
    }
  },
  computed: {
    importExcelUrl: function() {
      return `${window._CONFIG['domianURL']}/${this.url.importExcelUrl}`
    }
  },
  methods: {
    clearList() {
      this.dataSource = []
      this.selectedRowKeys = []
      this.ipagination.current = 1
    },
    handleGeneratePicking() {
      if (this.selectedRowKeys.length === 0) {
        this.$message.warning('请选择要生成领料单的工单')
        return
      }
      this.$refs.genPicking.visible = true
      this.$refs.genPicking.loadData(this.selectionRows)
    },
    handleGenerateProductIn() {
      if (this.selectedRowKeys.length === 0) {
        this.$message.warning('请选择要生成入库单的工单')
        return
      }
      this.$refs.genProductIn.visible = true
      this.$refs.genProductIn.loadData(this.selectionRows)
    },
    handleGenerateRework() {
      let v = false
      this.$refs.formRef.validate(valid => {
        if (valid) {
          v = true
        }
      })
      if (!v) {
        return
      }
      let body = {
        step: this.modal.rework.step,
        productionMissionId: this.mainId,
        productionMissionSerial: this.sourceSerial,
        department: this.modal.rework.depart,
        outputWarehouse: this.modal.rework.pikingWarehouse,
        goodWarehouse: this.modal.rework.goodWarehouse,
        defectiveWarehouse: this.modal.rework.defectiveWarehouse,
        scrapWarehouse: this.modal.rework.scrapWarehouse
      }
      postAction(this.url.generateRework, body, { params: { productOrderIds: this.selectedRowKeys.join(',') } })
        .then(res => {
          if (res.success) {
            this.$message.success('生成返工单成功')
            Modal.confirm({
              title: '提示',
              content: res.message + '是否跳转',
              okText: '确定',
              cancelText: '取消',
              onOk: () => {
                this.$router.push({
                  path: '/production/HfReworkList'
                })
              }
            })
          } else {
            this.$message.error(res.message)
          }
        })
        .finally(() => {
          this.modal.rework.visible = false
        })
    }
  }
}
</script>
<style scoped>
@import '~@assets/less/common.less';
</style>
